# -*- coding: utf-8 -*-
# created on 2016/12/22

from sympy import sympify, Eq
from sympy.core.function import UndefinedFunction
from mathsolver.functions.base import BaseFunction, BasePieceFunc, new_latex
from mathsolver.functions.hanshu.helper import check_jiou, check_inter, get_symmetric_interval


def jihanshu_dingyishi(expr):
    """hs054.奇函数的定义式

    :param expr f(x) 或者 f(g(x))
    :return -f(-x)=f(x) 或者 -f(g(-x)) = f(g(x))
    """
    if isinstance(expr.args[0].func, UndefinedFunction):
        symbol = expr.args[0].args[0]
    else:
        symbol = expr.args[0]

    res = Eq(expr, -expr.subs(symbol, -symbol))
    step = "由奇函数的定义可知 %s" % new_latex(res)
    return res, step


def ouhanshu_dingyishi(expr):
    """hs055.偶函数的定义式

    :param expr f(x) 或者 f(g(x))
    :return f(-x)=f(x) 或者 f(g(-x)) = f(g(x))
    """
    if isinstance(expr.args[0].func, UndefinedFunction):
        symbol = expr.args[0].args[0]
    else:
        symbol = expr.args[0]

    res = Eq(expr, expr.subs(symbol, -symbol))
    step = "由偶函数的定义可知 %s" % new_latex(res)
    return res, step


class JiOuJieXiShi(BaseFunction):
    """根据函数奇偶性求解析式"""

    def solver(self, *args):
        # 处理输入
        jiou, dd, func = check_jiou(args[0]), args[1], args[2]
        dd = check_inter(dd)

        expr, var, name = func.expression, func.var, func.name
        f_x = sympify(str(name) + '(' + str(var) + ')')  # f(x)
        f_negx = sympify(str(name) + '(-' + str(var) + ')')  # f(-x)

        # D1
        if len(args) == 4:
            dd1 = check_inter(args[3])
        else:
            dd1 = get_symmetric_interval(dd)

        expr_neg_x = expr.subs(var, -var)  # f(-x) 的表达式
        self.steps.append(["设 %s ∈ %s, 则 %s ∈ %s" % (new_latex(var), dd1, new_latex(-var), dd),
                           "则 f(-x) = %s" % new_latex(expr_neg_x)])

        # 根据奇偶性得到定义式
        if '奇' in jiou:
            relation, buzou = jihanshu_dingyishi(f_x)
        else:
            relation, buzou = ouhanshu_dingyishi(f_x)
        self.steps.append(["", buzou])

        # 结果
        res = relation.rhs.subs(f_negx, expr_neg_x)
        self.steps.append(["", "所以当 %s ∈ %s 时，f(x) = %s" % (var, dd1, new_latex(res))])
        self.output.append(BasePieceFunc({"var": "x", "name": "f", "type": "",
                                          "expression": [[expr, dd], [res, dd1]]}))
        self.label.add("根据函数奇偶性求解析式")
        return self


if __name__ == '__main__':
    pass
    # print "===================== 奇、偶函数的定义式 ====================="
    # print jihanshu_dingyishi(sympify('f(h(x))'))
    # print jihanshu_dingyishi(sympify('t(x)'))
    # print ouhanshu_dingyishi(sympify(' f(h(x))'))
    # print ouhanshu_dingyishi(sympify('t(x)'))
    #
    # print "===================== 根据函数奇偶性求解析式 ====================="
    # print jiou_jiexishi().solver(
    #     sympify('Eq(f(x), -f(-x))'), BaseIneq(["x", ">", "0"]),
    #     BaseFunc({'name': 'f', 'var': 'x', 'type': '', 'expression': 'x**2 + 1/x'})).output[0].printing()
    #
    # print jiou_jiexishi().solver(
    #     sympify('Eq(f(x), f(-x))'), BaseIneq(["x", ">=", "0"]),
    #     BaseFunc({'name': 'f', 'var': 'x', 'type': '', 'expression': 'x**2 - x + 1'})).output[0].printing()
    #
    # print jiou_jiexishi().solver(
    #     sympify('Eq(f(x), -f(-x))'), BaseIneq(["x", ">", "0"]),
    #     BaseFunc({'name': 'f', 'var': 'x', 'type': '', 'expression': 'sqrt(x) + 1'}),
    #     BaseIneq(["x", "<", "0"])).output[0].printing()
